﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace SSOClient.Config
{
    public class SSOConfig
    {
        private static readonly object _lockKey = new object();
        private static SSOConfig _instance = null;

        protected SSOConfig()
        { }

        public static SSOConfig Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lockKey)
                    {
                        if (_instance == null)
                        {
                            _instance = new SSOConfig();
                        }
                    }
                }
                return _instance;
            }
        }

        public string DefaultUrl
        {
            get
            {
                string returnValue = SSOSettingsSection.GetValue("defaultUrl");
                if (String.IsNullOrEmpty(returnValue))
                {
                    throw new ConfigurationErrorsException("The value of defaultUrl element is not config.");
                }
                return returnValue;
            }
        }

        public string LogoutUrl
        {
            get
            {
                string returnValue = SSOSettingsSection.GetValue("logoutUrl");
                if (String.IsNullOrEmpty(returnValue))
                {
                    throw new ConfigurationErrorsException("The value of logoutUrl element is not config.");
                }
                return returnValue;
            }
        }

        public string ServiceUrl
        {
            get
            {
                string returnValue = SSOSettingsSection.GetValue("serviceUrl");
                if (String.IsNullOrEmpty(returnValue))
                {
                    throw new ConfigurationErrorsException("The value of serviceUrl element is not config.");
                }
                return returnValue;
            }
        }

        public string SingleLoginUrl
        {
            get
            {
                string returnValue = SSOSettingsSection.GetValue("singleLoginUrl");
                if (String.IsNullOrEmpty(returnValue))
                {
                    throw new ConfigurationErrorsException("The value of singleLoginUrl element is not config.");
                }
                return returnValue;
            }
        }

        public int SiteID
        {
            get
            {

                string returnValue = SSOSettingsSection.GetValue("siteId");
                if (String.IsNullOrEmpty(returnValue))
                {
                    throw new ConfigurationErrorsException("The value of siteId element is not config.");
                }
                int temp;
                if (!Int32.TryParse(returnValue, out temp))
                {
                    throw new ConfigurationErrorsException("The value of siteId element is error.");
                }
                return temp;
            }
        }
    }
}
